package com.xzs.kit.base.aspect;

import com.xzs.kit.base.exception.ServiceException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;

/**
 * @Description
 * @author: Victor.Xiao
 * @create: 2018-09-03 16:06
 **/
public class LogAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);

    public Object around(ProceedingJoinPoint pjd) throws Throwable{
        Object result = null;
        String methodName = pjd.getSignature().getName();
        String className = pjd.getTarget().getClass().getName();
        Object[] obj =pjd.getArgs();
        try {
            if(null != obj && obj.length != 0){
                List<Object> list = Arrays.asList(obj);
                LOGGER.debug("{}.{}参数为:{}",className, methodName,list);
            }else{
                LOGGER.debug("{}.{}无参调用",className ,methodName);
            }
            //执行目标方法
            result = pjd.proceed();
            return result;
        } catch (Throwable e) {
            LOGGER.error("{}.{}调用异常:{}",className , methodName ,e);
            if(e instanceof ServiceException){
                throw e;
            }
            throw new RuntimeException(e);
        }finally{
            LOGGER.debug("{}.{}返回结果：{}",className , methodName ,result);
        }
    }
}
